

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sma.iss.IssUtil;
import sma.iss.IssVariable;
import au.com.bytecode.opencsv.CSVWriter;

public class SimulationRun {

	/** Logger for this class. */
	private final static Logger LOGGER = LoggerFactory.getLogger(SimulationRun.class);
	
	/** -rendering parameter. */
	private final static boolean RENDERING = true;

	/** All values of beta to be tested. */
	private final static double[] ALL_BETA = new double[] { 70, -72, 72, 74, -70, -74 };

	/** All values of beta for system tests. */
	private final static double[] ALL_SYSTEM_BETA = new double[] { 71, 73, 75, -71, -73, -75 };

	@Test
	public void testBeta75() throws IOException {
		LOGGER.info("testBeta75");
		this.testInternal(IssUtil.BETA_DEFAULT);

	}

	@Test
	public void testBeta74() throws IOException {
		LOGGER.info("testBeta74");
		this.testInternal(74);

	}

	@Test
	public void testBeta70() throws IOException {
		LOGGER.info("testBeta70");
		this.testInternal(70);
	}

	@Test
	public void testBetaNeg() throws IOException {
		LOGGER.info("testBetaNeg");

		this.testInternal(-72);

	}

	@Test
	public void testAllBeta() throws IOException {
		this.testBetaArray(ALL_BETA);
		// for (int i = 0; i < ALL_BETA.length; i++) {
		// double betaToTest = ALL_BETA[i];
		// LOGGER.info("testAllBeta, beta={}, remaining {}", betaToTest, ALL_BETA.length - i);
		// this.testInternal(betaToTest);
		// }
	}

	@Test
	public void testAllSystemBeta() throws IOException {
		this.testBetaArray(ALL_SYSTEM_BETA);
	}

	private void testBetaArray(double[] betaArray) throws IOException {
		for (int i = 0; i < betaArray.length; i++) {
			double betaToTest = betaArray[i];
			LOGGER.info("testBetaArray, beta={}, remaining {}", betaToTest, ALL_BETA.length - i);
			this.testInternal(betaToTest);
		}
	}

	private void testInternal(double beta) throws IOException {
		// the object to test
		IssVariable impl = new IssVariable();
		double yaw = impl.getInitialOrientation(beta);

		String csvFileName = "target/" + impl.getClass().getSimpleName() + ".csv";
		
		CSVWriter writer = new CSVWriter(new FileWriter(csvFileName), ',', CSVWriter.NO_QUOTE_CHARACTER);
		// ArrayList<String> firstLine = new ArrayList<String>();
		// firstLine.add("#firstLine");
		writer.writeNext(new String[] { "#firstLine" });
//		writer.setWriter(new FileWriter(csvFileName));

		String[] lineStr = new String[22];
		lineStr[0] = Double.toString(beta);
		lineStr[1] = Double.toString(yaw);

		for (int minute = 0; minute < 92; minute++) {
			double[] line = impl.getStateAtMinute(minute);
			// translate table
			for (int i = 0; i < line.length; i++) {
				double d = line[i];
				lineStr[i + 2] = Double.toString(d);
			}
			writer.writeNext(lineStr);
		}
	
		// writer.

		writer.flush();
		writer.close();

		LOGGER.info("csv created : " + csvFileName);



		List<String> lstParams = new ArrayList<String>();
		String[] arg = new String[] { "-beta", Double.toString(beta), "-csv", csvFileName };
		lstParams.addAll(Arrays.asList(arg));

		if (RENDERING) {
			lstParams.add("-rendering");
		}
		ISSVis.main(lstParams.toArray(new String[] {}));
		LOGGER.info("end");
	}

}
